/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Canadian Weather.
 *
 * The Initial Developer of the Original Code is
 *   George Bradt.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   George Bradt.
 *
 * Credits:
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

var textToXml = require('textToXml')
var data = require('sdk/self').data;

// A typical site data file.
var xmlObject = new textToXml.convert(data.load('xmlTest/siteDataComplete.txt'));
var rootElement = xmlObject.rootElement;

// Note that the tests are run in alphabetical order, eg. 'testLicense' before
// 'testRoot'. If an element is needed for a later test, use an appropriate name
// eg. 'test location' instead of 'testLocation'.
var locationElement, currentConditionsElement, forecastGroupElement;

// Test the root element.
exports['testRoot'] = function(assert) {
    assert.ok(rootElement.name === 'siteData', 'root is ok');
}

// Test the license element.
exports['testLicense'] = function(assert) {
    let licenseElement = rootElement.childElement('license');
    assert.ok(licenseElement !== null, 'license is present');
}

// Test the dateTime elements.
exports['testDateTimeRoot'] = function(assert) {
    let dateTimeRootElementArray = rootElement.childElementsNamed('dateTime');
    
    if ( dateTimeRootElementArray.length !== 2) {
        console.log('');
        console.log('******************************************************');
        console.log('Number of root dateTime elements: ' + dateTimeRootElementArray.length);
        console.log('******************************************************');
        console.log('');
    }
    
    assert.ok(dateTimeRootElementArray.length === 2, 'correct number of dateTime elements');
}

// Test the location element. locationElement needed later.
exports['test location'] = function(assert) {
    locationElement = rootElement.childElement('location');
    assert.ok(locationElement !== null, 'location is present');
}

// Test the warnings element.
exports['testWarnings'] = function(assert) {
    let warningsElement = rootElement.childElement('warnings');
    assert.ok(warningsElement !== null, 'warnings is present');
}

// Test the currentConditions element. currentConditionsElement needed later.
exports['test currentConditions'] = function(assert) {
    currentConditionsElement = rootElement.childElement('currentConditions');
    assert.ok(currentConditionsElement !== null, 'current conditions is present');
}

// Test the forecastGroup element. forecastGroupElement needed later.
exports['test forecastGroup'] = function(assert) {
    forecastGroupElement = rootElement.childElement('forecastGroup');
    assert.ok(forecastGroupElement !== null, 'forecast group is present');
}

// Test the yesterdayConditions element.
exports['testYesterdayConditions'] = function(assert) {
    let yesterdayConditionsElement = rootElement.childElement('yesterdayConditions');
    assert.ok(yesterdayConditionsElement !== null, 'yesterday conditions is present');
}

// Test the riseSet element.
exports['testRiseSet'] = function(assert) {
    let riseSetElement = rootElement.childElement('riseSet');
    assert.ok(riseSetElement !== null, 'rise/set is present');
}

// Test the almanac element.
exports['testAlmanac'] = function(assert) {
    let almanacElement = rootElement.childElement('almanac');
    assert.ok(almanacElement !== null, 'almanac is present');
}

// Test the location element's children.

// Test the continent element.
exports['testContinent'] = function(assert) {
    let continentElement = locationElement.childElement('continent');
    assert.ok(continentElement !== null, 'continent is present');
}

// Test the country element.
exports['testCountry'] = function(assert) {
    let countryElement = locationElement.childElement('country');
    assert.ok(countryElement !== null, 'country is present');
}

// Test the province element.
exports['testProvince'] = function(assert) {
    let provinceElement = locationElement.childElement('province');
    assert.ok(provinceElement !== null, 'province is present');
}

// Test the name element.
exports['testName'] = function(assert) {
    let nameElement = locationElement.childElement('name');
    assert.ok(nameElement !== null, 'name is present');
}

// Test the region element.
exports['testRegion'] = function(assert) {
    let regionElement = locationElement.childElement('region');
    assert.ok(regionElement !== null, 'region is present');
}

// Test the currentConditions element's children.

// Test the station element.
exports['testStation'] = function(assert) {
    let stationElement = currentConditionsElement.childElement('station');
    assert.ok(stationElement !== null, 'station is present');
}

// Test the dateTime element.
exports['testDateTimeCC'] = function(assert) {
    let dateTimeCCElementArray = currentConditionsElement.childElementsNamed('dateTime');
    
    if ( dateTimeCCElementArray.length !== 2) {
        console.log('');
        console.log('******************************************************');
        console.log('Number of current conditions dateTime elements: ' + dateTimeCCElementArray.length);
        console.log('******************************************************');
        console.log('');
    }
    
    assert.ok(dateTimeCCElementArray.length === 2, 'correct number of dateTime elements');
}

// Test the condition element.
exports['testCondition'] = function(assert) {
    let conditionElement = currentConditionsElement.childElement('condition');
    assert.ok(conditionElement !== null, 'condition is present');
}

// Test the iconCode element.
exports['testIconCode'] = function(assert) {
    let iconCodeElement = currentConditionsElement.childElement('iconCode');
    assert.ok(iconCodeElement !== null, 'icon code is present');
}

// Test the temperature element.
exports['testTemperature'] = function(assert) {
    let temperatureElement = currentConditionsElement.childElement('temperature');
    assert.ok(temperatureElement !== null, 'temperature is present');
}

// Test the dewpoint element.
exports['testDewpoint'] = function(assert) {
    let dewpointElement = currentConditionsElement.childElement('dewpoint');
    assert.ok(dewpointElement !== null, 'dewpoint is present');
}

// Test the pressure element.
exports['testPressure'] = function(assert) {
    let pressureElement = currentConditionsElement.childElement('pressure');
    assert.ok(pressureElement !== null, 'pressure is present');
}

// Test the visibility element.
exports['testVisibility'] = function(assert) {
    let visibilityElement = currentConditionsElement.childElement('visibility');
    assert.ok(visibilityElement !== null, 'visibility is present');
}

// Test the wind element.
exports['testWind'] = function(assert) {
    let windElement = currentConditionsElement.childElement('wind');
    assert.ok(windElement !== null, 'wind is present');
}

// Test the forecastGroup element's children.

// Test the dateTime element.
exports['testDateTimeFG'] = function(assert) {
    let dateTimeFGElementArray = forecastGroupElement.childElementsNamed('dateTime');
    
    if ( dateTimeFGElementArray.length !== 2) {
        console.log('');
        console.log('******************************************************');
        console.log('Number of forecast group dateTime elements: ' + dateTimeFGElementArray.length);
        console.log('******************************************************');
        console.log('');
    }
    
    assert.ok(dateTimeFGElementArray.length === 2, 'correct number of dateTime elements');
}

// Test the regionalNormals element.
exports['testRegionalNormals'] = function(assert) {
    let regionalNormalsElement = forecastGroupElement.childElement('regionalNormals');
    assert.ok(regionalNormalsElement !== null, 'regional normals is present');
}

// Test the forecast element.
exports['testForecast'] = function(assert) {
    let forecastElement = forecastGroupElement.childElement('forecast');
    assert.ok(forecastElement !== null, 'forecast is present');
}

require('sdk/test').run(exports);
